/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.service.impl;

import com.bifrost.entity.Media;
import com.bifrost.entity.ProductSku;
import com.bifrost.entity.SkuMediaMap;
import com.bifrost.repository.SkuMediaMapRepository;
import com.bifrost.service.SkuMediaMapService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author joewee
 * @version 1.0.0
 * @Date 2017/11/13 14:11
 */
@Service
public class SkuMediaMapServiceImpl implements SkuMediaMapService {
    @Autowired
    SkuMediaMapRepository skuMediaMapRepository;

    /**
     * 获取sku所有图片素材
     *
     * @param sku
     * @return
     */
    @Override
    public List<Media> getAllMedia(ProductSku sku) {
        List<SkuMediaMap> maps = skuMediaMapRepository.findAllByProductSku(sku);
        if (maps != null && !maps.isEmpty()) {
            return maps.stream().map(SkuMediaMap::getMedia).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 获取sku商品图
     *
     * @param sku
     * @return
     */
    @Override
    public List<Media> getPrimaryMedia(ProductSku sku) {
        List<SkuMediaMap> maps = skuMediaMapRepository.findAllByProductSkuAndMapKeyOrderBySortOrderAsc(sku, "PRIMARY");
        if (maps != null && !maps.isEmpty()) {
            return maps.stream().filter(skuMediaMap -> skuMediaMap.getMedia() != null).map(SkuMediaMap::getMedia).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 获取sku图文描述
     *
     * @param sku
     * @return
     */
    @Override
    public List<Media> getDescriptionMedia(ProductSku sku) {
        List<SkuMediaMap> skuMediaMapList = skuMediaMapRepository.findAllByProductSkuAndMapKeyOrderBySortOrderAsc(sku, "DESCRIPTION");
        if (CollectionUtils.isNotEmpty(skuMediaMapList)) {
            return skuMediaMapList.stream().filter(skuMediaMap -> skuMediaMap.getMedia() != null).map(SkuMediaMap::getMedia).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 查询商品默认media
     *
     * @param sku
     * @return
     */
    @Override
    public Media getDefaultPrimaryMedia(ProductSku sku) {
        List<Media> medias = getPrimaryMedia(sku);
        if (medias != null && !medias.isEmpty()) {
            return medias.get(0);
        }
        return null;
    }

    @Override
    public Media getDefaultPrimaryMedia(Long skuId) {
        List<SkuMediaMap> maps = skuMediaMapRepository.findAllByProductSkuAndMapKeyOrderBySortOrderAsc(skuId, "PRIMARY");
        if (maps != null && !maps.isEmpty()) {
            return maps.stream().map(SkuMediaMap::getMedia).collect(Collectors.toList()).get(0);
        }
        return null;
    }
}
